package com.callas.nine.HoneyCombLifeGame;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;

public class HoneyCombLifeGameActivity extends Activity {
	final static String TAG = "Honeybeeee";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SurfaceView sfv = (SurfaceView)findViewById(R.id.SurfaceView01);
        sfv.setClickable(true);
        sfv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int X = (int)v.getX();
				int Y = (int)v.getY();
				Log.d(TAG,"Click!!!" + "X=" + X + " : Y=" + Y);

			}
		});
        sfv.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				SurfaceView svf = (SurfaceView)v;
				Canvas canvas = svf.getHolder().lockCanvas();
				canvas.drawColor(Color.WHITE);
				Paint paint = new Paint();
				paint.setAntiAlias(true);
				
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					createCircle(event,canvas,paint,MotionEvent.ACTION_DOWN);
					break;
				case MotionEvent.ACTION_MOVE:
					createCircle(event,canvas,paint,MotionEvent.ACTION_MOVE);
					break;
				case MotionEvent.ACTION_UP:
					break;
				default:
				}
				
				svf.getHolder().unlockCanvasAndPost(canvas);
				return false;
			}
		});
    }
    
    private void createCircle(MotionEvent event,Canvas canvas,Paint paint,int action){
    	int[] colorArr = {Color.BLUE,
    					  Color.GREEN,
    					  Color.CYAN,
    					  Color.MAGENTA,
    					  Color.BLACK,
    					  Color.RED,
    					  Color.DKGRAY,
    					  Color.YELLOW,
    					  Color.GRAY,
    					  Color.LTGRAY};
    	String[] stringArr = {"いち","にー","さん","しー","ごー","ろく","なな","はち","くー","じゅ"};

    	int X = 0;
    	int Y = 0;
    	for(int i=0;i<event.getPointerCount();i++){
    		int HS = event.getHistorySize();
    		int PC = event.getPointerCount();
    		int pos = HS/PC;
    		Log.d(TAG,"[pos:HS:PC] = [" + pos + ":" + HS + ":" + PC +"]");
    		if (action == MotionEvent.ACTION_MOVE && pos > 0){
    			X = (int)event.getHistoricalX(i, pos);
    			Y = (int)event.getHistoricalY(i, pos);
    		} else {
    			X = (int)event.getX(i);
    			Y = (int)event.getY(i);
    		}
    		Y -= 80;
    		paint.setColor(colorArr[i]);
    		canvas.drawCircle(X, Y, 50, paint);
    		paint.setColor(Color.WHITE);
    		canvas.drawCircle(X, Y, 45, paint);
    		paint.setColor(Color.BLACK);
    		paint.setTextSize(24);
    		canvas.drawText(stringArr[i], X-20, Y+8, paint);
    	}

    }
}